package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.TwsUseRecord;
import com.freight.tws.service.ITwsUseRecordService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 操作日志Controller
 * 
 * @author wjx
 * @date 2023-03-01
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/record" )
public class TwsUseRecordController extends BaseController
{
    private final ITwsUseRecordService iTwsUseRecordService;

    /**
     * 查询操作日志列表
     */
    @PreAuthorize(hasPermi="tws:record:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, TwsUseRecord twsUseRecord)
    {
        LambdaQueryWrapper<TwsUseRecord> lqw = new LambdaQueryWrapper<TwsUseRecord>();
        if (StringUtils.isNotBlank(twsUseRecord.getOperator())) {
            lqw.eq(TwsUseRecord::getOperator, twsUseRecord.getOperator());
        }
        if (StringUtils.isNotBlank(twsUseRecord.getOperateContent())) {
            lqw.eq(TwsUseRecord::getOperateContent, twsUseRecord.getOperateContent());
        }
        if (twsUseRecord.getOperateTime() != null) {
            lqw.eq(TwsUseRecord::getOperateTime, twsUseRecord.getOperateTime());
        }
        lqw.eq(TwsUseRecord::getOrderId, twsUseRecord.getOrderId());
        lqw.orderByDesc(TwsUseRecord::getCreateTime);
        IPage<TwsUseRecord> list = iTwsUseRecordService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出操作日志列表
     */
    @PreAuthorize(hasPermi = "tws:record:export")
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TwsUseRecord twsUseRecord) throws IOException
    {
        LambdaQueryWrapper<TwsUseRecord> lqw = new LambdaQueryWrapper<TwsUseRecord>(twsUseRecord);
        List<TwsUseRecord> list = iTwsUseRecordService.list(lqw);
        ExcelUtil<TwsUseRecord> util = new ExcelUtil<TwsUseRecord>(TwsUseRecord. class);
        util.exportExcel(response, list, "record");
    }

    /**
     * 获取操作日志详细信息
     */
    @PreAuthorize(hasPermi="tws:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iTwsUseRecordService.getById(id));
    }

    /**
     * 新增操作日志
     */
    @PreAuthorize(hasPermi="tws:record:add")
    @Log(title = "操作日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TwsUseRecord twsUseRecord) {
        return toAjax(iTwsUseRecordService.save(twsUseRecord) ? 1 : 0);
    }

    /**
     * 修改操作日志
     */
    @PreAuthorize(hasPermi="tws:record:edit")
    @Log(title = "操作日志" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TwsUseRecord twsUseRecord) {
        return toAjax(iTwsUseRecordService.updateById(twsUseRecord) ? 1 : 0);
    }

    /**
     * 删除操作日志
     */
    @PreAuthorize(hasPermi="tws:record:remove")
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iTwsUseRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
